<?php

namespace maotool\orm\mapper;

use JsonSerializable;

class PageInfo implements JsonSerializable
{

    private $total;

    private $list;

    private $pageNum;

    private $pageSize;

    private $size;

    private $startRow;

    private $endRow;

    private $pages;

    private $prePage;

    private $nextPage;

    private $isFirstPage;

    private $isLastPage;

    private $hasPreviousPage;

    private $hasNextPage;

    public function __construct($list = null)
    {
        $this->list = $list;
        $page = PageHelper::getLocalPage();
        if (isset($page)) {
            $this->total = $page->getTotal();
            $this->pageSize = $page->getPageSize();
            $this->pageNum = $page->getPageNum();
            $this->prePage = $page->getPageNum() - 1 <= 0 ? 1 : $page->getPageNum() - 1;
            $this->nextPage = $page->getPageNum() + 1;
            $this->hasPreviousPage = true;
            if ($this->total >= ($this->pageSize * $this->nextPage)) {
                $this->hasNextPage = true;
            } else {
                $this->hasNextPage = false;
            }
            PageHelper::clearPage();
        }
    }

    /**
     * @return int
     */
    public function getTotal()
    {
        return $this->total;
    }

    /**
     * @param int $total
     */
    public function setTotal($total)
    {
        $this->total = $total;
    }

    /**
     * @return mixed|null
     */
    public function getList()
    {
        return $this->list;
    }

    /**
     * @param mixed|null $list
     */
    public function setList($list)
    {
        $this->list = $list;
    }

    /**
     * @param mixed $pageNum
     * @return PageInfo
     */
    public function setPageNum($pageNum)
    {
        $this->pageNum = $pageNum;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getPageSize()
    {
        return $this->pageSize;
    }

    /**
     * @param mixed $pageSize
     */
    public function setPageSize($pageSize)
    {
        $this->pageSize = $pageSize;
    }

    /**
     * @return mixed
     */
    public function getPageNum()
    {
        return $this->pageNum;
    }

    /**
     * @return mixed
     */
    public function getSize()
    {
        return $this->size;
    }

    /**
     * @param mixed $size
     */
    public function setSize($size)
    {
        $this->size = $size;
    }

    /**
     * @return mixed
     */
    public function getPages()
    {
        return $this->pages;
    }

    /**
     * @param mixed $pages
     */
    public function setPages($pages)
    {
        $this->pages = $pages;
    }

    /**
     * @return mixed
     */
    public function getStartRow()
    {
        return $this->startRow;
    }

    /**
     * @param mixed $startRow
     */
    public function setStartRow($startRow)
    {
        $this->startRow = $startRow;
    }

    /**
     * @return mixed
     */
    public function getEndRow()
    {
        return $this->endRow;
    }

    /**
     * @param mixed $endRow
     */
    public function setEndRow($endRow)
    {
        $this->endRow = $endRow;
    }

    /**
     * @return mixed
     */
    public function getPrePage()
    {
        return $this->prePage;
    }

    /**
     * @param mixed $prePage
     */
    public function setPrePage($prePage)
    {
        $this->prePage = $prePage;
    }

    /**
     * @return mixed
     */
    public function getNextPage()
    {
        return $this->nextPage;
    }

    /**
     * @param mixed $nextPage
     */
    public function setNextPage($nextPage)
    {
        $this->nextPage = $nextPage;
    }

    /**
     * @return mixed
     */
    public function getIsFirstPage()
    {
        return $this->isFirstPage;
    }

    /**
     * @param mixed $isFirstPage
     */
    public function setIsFirstPage($isFirstPage)
    {
        $this->isFirstPage = $isFirstPage;
    }

    /**
     * @return mixed
     */
    public function getIsLastPage()
    {
        return $this->isLastPage;
    }

    /**
     * @param mixed $isLastPage
     */
    public function setIsLastPage($isLastPage)
    {
        $this->isLastPage = $isLastPage;
    }

    /**
     * @return mixed
     */
    public function getHasPreviousPage()
    {
        return $this->hasPreviousPage;
    }

    /**
     * @param mixed $hasPreviousPage
     */
    public function setHasPreviousPage($hasPreviousPage)
    {
        $this->hasPreviousPage = $hasPreviousPage;
    }

    /**
     * @return mixed
     */
    public function getHasNextPage()
    {
        return $this->hasNextPage;
    }

    /**
     * @param mixed $hasNextPage
     */
    public function setHasNextPage($hasNextPage)
    {
        $this->hasNextPage = $hasNextPage;
    }

    public function jsonSerialize()
    {
        return [
            "total" => $this->total,
            "list" => $this->list,
            "pageNum" => $this->pageNum,
            "pageSize" => $this->pageSize,
            "size" => $this->size,
            "startRow" => $this->startRow,
            "endRow" => $this->endRow,
            "pages" => $this->pages,
            "prePage" => $this->prePage,
            "nextPage" => $this->nextPage,
            "isFirstPage" => $this->isFirstPage,
            "isLastPage" => $this->isLastPage,
            "hasPreviousPage" => $this->hasPreviousPage,
            "hasNextPage" => $this->hasNextPage,
        ];
    }

}